package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.pojo.*;
import com.itheima.reggie.service.*;
import com.itheima.reggie.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    UserService userService;
    @Autowired
    AddressBookService addressBookService;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    OrdersMapper ordersMapper;

    /**
     * 用户下单
     *
     * @param orders
     */
    @Override
    public void saveOrder(Orders orders) {
        //四部分:
        // TODO: 2022/8/22 创建user,addressBook, 对象
        //便于封装
        //创建user用户对象
        Long userId = BaseContext.get();
        User user = userService.getById(userId);
        //创建地址对象
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        //设置id值
        long orderId = IdWorker.getId();
        //定义总金额对象
        BigDecimal total = new BigDecimal(0);
        // TODO: 2022/8/22 封装订单表和订单详情表数据

        //设置订单详情表数据
        //设置容器
        List<OrderDetail> orderDetails = new ArrayList<>();
        //查询用户购物车
        List<ShoppingCart> shoppingCartList = shoppingCartService.query().eq("user_id", userId).list();

        for (ShoppingCart cart : shoppingCartList) {

            //创建一个OrderDetail
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderId);//订单id
            orderDetail.setNumber(cart.getNumber());//设置数量
            orderDetail.setDishFlavor(cart.getDishFlavor());//口味
            orderDetail.setDishId(cart.getDishId());//菜品id
            orderDetail.setSetmealId(cart.getSetmealId());//套餐id
            orderDetail.setName(cart.getName());//名称
            orderDetail.setImage(cart.getImage());//图片
            orderDetail.setAmount(cart.getAmount());//金额 单价

            orderDetails.add(orderDetail);
            //计算金额
            BigDecimal mn = cart.getAmount().multiply(new BigDecimal(cart.getNumber()));
            total = total.add(mn);

        }


        //设置订单表数据
        orders.setId(orderId);
        orders.setNumber(String.valueOf(orderId));//订单编号
        orders.setOrderTime(LocalDateTime.now());//下单时间
        orders.setCheckoutTime(LocalDateTime.now());//支付时间
        orders.setStatus(2);//状态 订单状态 1待付款，2待派送，3已派送，4已完成，5已取消
        orders.setUserId(userId);//用户id
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());//收获人
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setAmount(total);// 总金额



        // TODO: 2022/8/22 保存数据
        //保存订单数据
        this.save(orders);
        //保存订单详情数据
        orderDetailService.saveBatch(orderDetails);

        // TODO: 2022/8/22 清空购物车
        //清空购物车
        shoppingCartService.update().eq("user_id",userId).remove();

    }

    /**
     * 订单分页
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<OrdersDto> pages(Integer page, Integer pageSize) {
        Page<OrdersDto> ordersDtoPage = new Page<>(page, pageSize);
        return ordersMapper.pages(ordersDtoPage);
    }
}
